//
//  UserExtensionViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/24.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "UserExtensionViewController.h"
#import "NavigationBarToggleTabView.h"
#import "LicensePlateTableViewCell.h"
#import "NewsTableViewCell.h"
#import "UserExtensionTableViewCell.h"
#import "RHTableView.h"
#import "MoreBlankTableViewHeaderFooterView.h"
#import "UserExtensionCustomerViewController.h"
#import "InsuranceBrokersContentViewController.h"
#import "V4TestLicensePlateContentViewController.h"

@interface UserExtensionViewController (){
    RHTableView *table_user;
    NSString *strType;
    
    BOOL boolRefreshData;
}

@end

@implementation UserExtensionViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (boolRefreshData) {
        boolRefreshData=NO;
        [self refreshTableViewData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    strType=@"1";
    [self navbarTitle:@" "];
    self.backButton.frameWidth=60;
    [self initTabelViews];
    [self initNavTopButtons];
}
-(void)initNavTopButtons{
    NavigationBarToggleTabView *toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-150)/2, 28, 150, 28)];
    [self.navView addSubview:toggleTab];
    __weak UserExtensionViewController *viewC=self;
    [toggleTab setTiltelArray:@[@{@"title":@"牌照分享",@"id":@"1"},@{@"title":@"资讯分享",@"id":@"2"}] toggleTab:^(NSDictionary *dicSelect) {
        strType=[dicSelect valueForJSONStrKey:@"id"];
        [viewC refreshTableViewData];
    }];
}

-(void)initTabelViews{
    table_user=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kContentHeight)];
    [table_user showRefresh:YES LoadMore:YES];
    table_user.delegate = self;
    table_user.dataSource = self;
    [table_user setBackgroundColor:rgbGray];
    [table_user setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:table_user];
    [table_user setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [table_user registerClass:[LicensePlateTableViewCell class] forCellReuseIdentifier:@"LicensePlateTableViewCell"];
    [table_user registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"NewsTableViewCell"];
    [table_user registerClass:[UserExtensionTableViewCell class] forCellReuseIdentifier:@"UserExtensionTableViewCell"];
    [table_user registerClass:[MoreBlankTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"MoreBlankTableViewHeaderFooterView"];
}
-(void)refreshTableViewData{
    krequestParam
    [dictparam setValue:strType forKey:@"type"];
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV3expand,dictparam.wgetParamStr];
    [table_user refresh];
}

#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return table_user.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:section];
    if ([[dic objectForJSONKey:@"people"] count]) {
        return 2;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        if ([strType isEqualToString:@"1"]) {
            NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.section];
            float fth=[self heightForLabel:kScreenWidth-50 font:fontTitle text:[dic valueForJSONStrKey:@"stitle"]];
            fth = fth>20?40:20;
            return fth+55;
        }
        return 90.5;
    }else{
        return 35;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.section];
    if (indexPath.row==0) {
        if ([strType isEqualToString:@"1"]) {
            LicensePlateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LicensePlateTableViewCell" forIndexPath:indexPath];
            [cell setValueForDictionary:dic indexPath:indexPath type:@"Extension"];
            return cell;
        }else{
            NewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
            [cell setValueForDictionary:dic indexPath:indexPath type:@"Extension"];
            return cell;
        }
    }else{
        UserExtensionTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UserExtensionTableViewCell" forIndexPath:indexPath];
        [cell setValueForDictionary:dic indexPath:indexPath type:@"Extension"];
        return cell;
    }
   
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.section];
    if (indexPath.row==0) {
        if ([strType isEqualToString:@"1"]) {
            [self pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
        }else{
            [self pushController:[InsuranceBrokersContentViewController class] withInfo:@"new" withTitle:@" " withOther:dic];
        }
    }else{
        NSMutableDictionary *dicTemp=[[NSMutableDictionary alloc] initWithDictionary:dic];
        [dicTemp setValue:strType forKey:@"type"];
        [self pushController:[UserExtensionCustomerViewController class] withInfo:@"" withTitle:@"推广详情" withOther:dicTemp];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 5.f;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return nil;
    }
    MoreBlankTableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MoreBlankTableViewHeaderFooterView"];
    return sectionView;
}

#pragma mark poprefresh
-(void)popRefreshData{
    boolRefreshData=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
